package com.example.demo.tools;

import com.example.demo.dao.ClassMemberDao;
import com.example.demo.dao.HomeworkDao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.pojo.ClassMember;
import com.example.demo.entity.pojo.Homework;
import com.example.demo.entity.pojo.User;
import com.example.demo.entity.vo.HomeworkDetailVO;
import com.example.demo.entity.vo.HomeworkListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业查询工具
 */
@Component
public class HomeworkTool {

    @Autowired
    private HomeworkDao homeworkDao;

    @Autowired
    private ClassMemberDao classMemberDao;

    @Autowired
    private UserDao userDao;

    /**
     * 查询作业信息
     */
    public Map<String, Object> queryHomework(Map<String, Object> params, String userId, String sessionId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            String action = (String) params.get("action");
            
            switch (action) {
                case "my":
                    // 查询我的作业
                    List<ClassMember> myMembers = classMemberDao.queryByUserId(userId);
                    if (myMembers.isEmpty()) {
                        result.put("homework", new ArrayList<>());
                        result.put("count", 0);
                        result.put("message", "您还没有加入任何班级，暂无作业");
                        break;
                    }

                    List<Integer> classIds = new ArrayList<>();
                    for (ClassMember member : myMembers) {
                        classIds.add(member.getClassId());
                    }

                    List<Homework> myHomework = homeworkDao.queryByClassIds(classIds);
                    List<HomeworkListVO> myHomeworkVOs = new ArrayList<>();
                    for (Homework homework : myHomework) {
                        User teacher = userDao.queryById(homework.getTeacherId());
                        String teacherName = (teacher != null) ? teacher.getUsername() : "未知教师";
                        myHomeworkVOs.add(new HomeworkListVO(homework, teacherName));
                    }
                    result.put("homework", myHomeworkVOs);
                    result.put("count", myHomeworkVOs.size());
                    break;
                    
                case "detail":
                    // 查询作业详情
                    Integer homeworkId = (Integer) params.get("homework_id");
                    if (homeworkId == null) {
                        throw new IllegalArgumentException("查询作业详情时必须提供作业ID");
                    }

                    Homework homework = homeworkDao.queryById(homeworkId);
                    if (homework == null) {
                        throw new IllegalArgumentException("作业不存在");
                    }

                    // 验证权限
                    ClassMember member = classMemberDao.queryByClassIdAndUserId(
                        homework.getClassId(), userId);
                    if (member == null) {
                        throw new IllegalArgumentException("您没有权限查看该作业");
                    }

                    // 查询教师信息并构建详情VO
                    User teacher = userDao.queryById(homework.getTeacherId());
                    String teacherName = (teacher != null) ? teacher.getUsername() : "未知教师";
                    HomeworkDetailVO homeworkDetail = new HomeworkDetailVO(homework, teacherName);

                    result.put("homework", homeworkDetail);
                    break;
                    
                case "by_class":
                    // 根据班级查询作业
                    Integer classId = (Integer) params.get("class_id");
                    if (classId == null) {
                        throw new IllegalArgumentException("查询班级作业时必须提供班级ID");
                    }

                    // 验证权限
                    ClassMember classMember = classMemberDao.queryByClassIdAndUserId(classId, userId);
                    if (classMember == null) {
                        throw new IllegalArgumentException("您不是该班级的成员，无法查看作业");
                    }

                    List<Homework> classHomework = homeworkDao.queryByClassId(classId);
                    List<HomeworkListVO> classHomeworkVOs = new ArrayList<>();
                    for (Homework homework1 : classHomework) {
                        User teacher1 = userDao.queryById(homework1.getTeacherId());
                        String teacherName1 = (teacher1 != null) ? teacher1.getUsername() : "未知教师";
                        classHomeworkVOs.add(new HomeworkListVO(homework1, teacherName1));
                    }
                    result.put("homework", classHomeworkVOs);
                    result.put("count", classHomeworkVOs.size());
                    break;
                    
                default:
                    throw new IllegalArgumentException("不支持的查询类型: " + action);
            }
            
        } catch (Exception e) {
            result.put("error", e.getMessage());
        }
        
        return result;
    }
}
